//
//  NSDictionary+uppercaseStringKey.m
//  RunTime
//
//  Created by 邢行 on 2018/11/15.
//  Copyright © 2018年 邢行. All rights reserved.
//

#import "NSDictionary+uppercaseStringKey.h"

@implementation NSDictionary (uppercaseStringKey)


- (id)keyCapters{
    NSLog(@"转换之前:%@",self);
    NSMutableDictionary *end = [NSMutableDictionary dictionary];
    [self enumerateKeysAndObjectsUsingBlock:^(NSString*  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSString *uppercaseStringKey = key.uppercaseString;
        //如果 value 是字典
        if ([obj isKindOfClass:[NSDictionary class]]) {
            obj = [obj keyCapters];
        }
        //如果 value 是数组
        if ([obj isKindOfClass:[NSArray class]]) {
            NSArray *temArr = [NSArray arrayWithArray:obj];
            NSMutableArray *endArr = [NSMutableArray array];
            [temArr enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [endArr addObject:[obj keyCapters]];
            }];
            obj = endArr;
        }
        //如果 value 是字符串
        [end setObject:obj forKey:uppercaseStringKey];
    }];
    NSLog(@"转换完成:%@",[self dictToString:end]);
    return end;
}


- (NSString *)dictToString:(NSDictionary *)dict{
    NSString *tem = @"";
    NSError *error = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    tem = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return tem;
}

@end
